﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using HalconDotNet;

namespace WaferImage
{
    class EvaluteImageCore
    {
        //接口说明：
        //srcdata   一幅灰度图像的数据
        //src_width  原始灰度图像的宽度
        //src_height 原始灰度图像的高度
        //evalute_value[1]~evalute_value[9]:图像被切成9个小块，为每块的平均灰度
        //evalue_averge[0]:为整福图像的平均灰度值
        //evalue_averge[10] 图像畸变比例
        //evalue_averge[11] 拉普拉斯值,得分越高图像越清晰
        //evalue_averge[12] sobel值,得分越高图像越清晰
        //evalue_averge[13] 标准差,值越大,差异性越大,图像越清晰
        //evalue_averge[14] 灰度最大值
        //evalue_averge[15] 灰度最小值
        /// <summary> (针对隐裂,脏污,表崩图像)返回图像的灰度值和各分区的灰度值
        /// </summary>
        /// <param name="srcdata">char* srcdata 一幅灰度图像的数据</param>
        /// <param name="srcwidth">原始灰度图像的宽度</param>
        /// <param name="srcheight">原始灰度图像的高度</param>
        /// <param name="evalute_value">evalue_averge[0]:为整福图像的平均灰度值
        /// evalute_value[1]~evalute_value[9]:图像被切成9个小块，为每块的平均灰度值。
        /// </param>
        /// <returns></returns>
        [DllImport("EvaluateImage.dll", CallingConvention = CallingConvention.StdCall)]
        //[DllImport("D:\\图片质量检测\\WaferImage\\TZEvaluteImage.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern int evalute_image(IntPtr srcdata, int srcwidth, int srcheight, double[] evalute_value);

        ///// <summary>针对侧崩图像
        ///// </summary>
        ///// <param name="img"></param>
        ///// <param name="width"></param>
        ///// <param name="height"></param>
        ///// <param name="meanValue"> 平均灰度值(检测区域)</param>
        ///// <param name="factor">整副图像的清晰度</param>
        ///// <returns></returns>
        //[DllImport("CheckModel.dll", CallingConvention = CallingConvention.StdCall)]
        //public static extern int UDCrackCheck(IntPtr img, int width, int height,ref double meanValue, ref double factor);

        ///// <summary>针对线痕图像
        ///// </summary>
        ///// <param name="imgsrc"></param>
        ///// <param name="width"></param>
        ///// <param name="height"></param>
        ///// <param name="th">整副图像的Otsu分割阈值</param>
        ///// <param name="ratio">宽度均匀性的比例值</param>
        ///// <param name="len">线痕有效区域的长度</param>
        ///// <returns></returns>
        //[DllImport("CheckModel.dll", CallingConvention = CallingConvention.StdCall)]   
        //public static extern int SawCheck(IntPtr imgsrc,int width,int height,ref int th,ref double ratio,ref int len);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="stdTh">标准灰度值200</param>
        /// <param name="stdFactor">标准清晰度1</param>
        /// <param name="meanValue">灰度值</param>
        /// <param name="factor">清晰度</param>
        /// <returns></returns>
        [DllImport("CheckModel.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern int UDCrackCheck(IntPtr img, int width, int height, double stdTh, double stdFactor, ref double meanValue, ref double factor);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="stdTh">标准灰度阈值100</param>
        /// <param name="stdRatio">标准长宽比0.3</param>
        /// <param name="th">灰度阈值</param>
        /// <param name="ratio">长宽比</param>
        /// <returns></returns>
        [DllImport("CheckModel.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern int SawCheck(IntPtr img, int width, int height, double stdTh, double stdRatio, ref int th, ref double ratio);
        public static void Bitmap2HObjectBpp8(Bitmap bmp, out HObject image)
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
                bmp.UnlockBits(srcBmpData);
            }
            catch (Exception ex)
            {
                image = null;
            }
        }
        public static double CalculGrayMean(Bitmap m_bitmap)
        {
            HObject ho_image;
            HObject ho_region;
            HTuple ho_Mean = new HTuple();
            HTuple ho_deviation = new HTuple();
            HOperatorSet.GenEmptyObj(out ho_image);
            HOperatorSet.GenEmptyObj(out ho_region);
            ho_image.Dispose();
            Bitmap2HObjectBpp8(m_bitmap, out ho_image);
            ho_region.Dispose();
            HOperatorSet.Threshold(ho_image, out ho_region, 0, 255);
            HOperatorSet.Intensity(ho_region, ho_image, out ho_Mean, out ho_deviation);
            ho_region.Dispose();
            ho_image.Dispose();
            double Meanvalue = ho_Mean[0];
            return Meanvalue;


        }

    
    }






}
